﻿package mortal.game.view.guide
{
    import Message.DB.Tables.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.net.rmi.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.task.data.*;

    public class GuideWhenNoOperation extends Object
    {
        private var _timer:SecTimer;
        private var _lastClickTime:int = 0;
        private static var _instance:GuideWhenNoOperation;

        public function GuideWhenNoOperation()
        {
            return;
        }// end function

        public function work() : void
        {
            if (this._timer == null)
            {
                this._timer = new SecTimer(3);
                this._timer.addListener(TimerType.ENTERFRAME, this.everySecondHandler);
            }
            if (!this._timer.running)
            {
                this._timer.start();
            }
            Global.stage.removeEventListener(MouseEvent.CLICK, this.clickStageHandler);
            Global.stage.addEventListener(MouseEvent.CLICK, this.clickStageHandler);
            return;
        }// end function

        private function clickStageHandler(param1) : void
        {
            this._lastClickTime = FrameManager.flashFrame.getTime();
            return;
        }// end function

        private function everySecondHandler(param1) : void
        {
            var _loc_5:TCopy = null;
            if (GameRMI.instance.isConnected == false)
            {
                return;
            }
            if (AutoGuideCache.isNotAutoAI)
            {
                return;
            }
            if (AIManager.isWorking)
            {
                return;
            }
            if (Cache.instance.task.isDrama)
            {
                return;
            }
            if (Cache.instance.role != null && Cache.instance.role.roleInfo != null && Cache.instance.role.roleInfo.life <= 0)
            {
                return;
            }
            if (!Game.isSceneInit())
            {
                return;
            }
            var _loc_2:* = FrameManager.flashFrame.getTime();
            var _loc_3:* = Math.max(this._lastClickTime, AIManager.endTime);
            var _loc_4:* = _loc_2 - _loc_3;
            if (_loc_4 >= GameConst.GuideCopyAutoFightTime)
            {
                _loc_5 = Cache.instance.copy.copyInfo;
                if (CopyUtil.needAutoFight(_loc_5))
                {
                    AIManager.onAIControl(AIType.AutoFight, -1);
                    return;
                }
            }
            if (!CopyUtil.isInCopy && _loc_4 >= 15000 && _loc_4 <= 20000)
            {
                if (Cache.instance.role.roleInfo.level <= 50)
                {
                    if (TaskUtil.isDoingGuideTask(10024))
                    {
                        GameProxy.copy.enterCopy(220);
                    }
                    else if (TaskUtil.isDoingGuideTask(10025))
                    {
                        GameProxy.copy.enterCopy(128);
                    }
                    else if (TaskUtil.isDoingGuideTask(10021))
                    {
                        GameProxy.copy.enterChallengeCopy(1, 1, 1);
                    }
                    else if (Cache.instance.task.getTaskByCode(11509, true) != null)
                    {
                        GameProxy.copy.enterChallengeCopy(1, 1, 2);
                    }
                    else if (TaskUtil.isDoingGuideTask(10029))
                    {
                        GameProxy.tower.enterTowerCopy(131, 1);
                    }
                }
            }
            if (Cache.instance.role.roleInfo.level < GameConst.AutoDoAutoFightLv)
            {
                return;
            }
            if (CopyUtil.isInCopy)
            {
                return;
            }
            if (PulseSharedObject.isTodayNotTips(TodayNoTipsConst.TodayNotAutoFight))
            {
                return;
            }
            if (CopyUtil.isInCopy)
            {
                return;
            }
            if (RolePlayer.instance.entityInfo.isDead)
            {
                return;
            }
            if (CopyUtil.isInAllEscortTask)
            {
                return;
            }
            if (_loc_2 - _loc_3 >= GameConst.GuideToSelfServerAutoFightTime)
            {
                if (GuideAutoFightWin.instance.isHide)
                {
                    GuideAutoFightWin.instance.show();
                }
                GuideAutoFightWin.instance.startWork();
            }
            return;
        }// end function

        private function stop() : void
        {
            if (this._timer != null)
            {
                this._timer.dispose(false);
                this._timer.stop();
                this._timer = null;
            }
            Global.stage.removeEventListener(MouseEvent.CLICK, this.clickStageHandler);
            return;
        }// end function

        public static function get instance() : GuideWhenNoOperation
        {
            if (_instance == null)
            {
                _instance = new GuideWhenNoOperation;
            }
            return _instance;
        }// end function

    }
}
